package com.macro.mall.portal.service.impl;

import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.dao.UmsCommentDao;
import com.macro.mall.portal.dao.UmsTagsCommentDao;
import com.macro.mall.portal.dao.UmsViewCommentDao;
import com.macro.mall.portal.domain.*;
import com.macro.mall.portal.service.UmsCommentService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author：Minxiao_Hao
 * @Description:
 * @name：UmsCommentServiceImpl
 * @Date：2024/10/26 13:50
 */
@Service
public class UmsCommentServiceImpl implements UmsCommentService {
    @Autowired
    private UmsCommentDao umsCommentDao;
    @Autowired
    private UmsViewCommentDao umsViewCommentDao;
    @Autowired
    private UmsTagsCommentDao umsTagsCommentDao;
    @Autowired
    private UmsMemberService umsMemberService;
    /**
     * 添加商品评论接口
     * @param commentDto 评论请求对象
     * @return 成功条数
     */
    @Override
    @Transactional
    public Integer addComment(CommentDto commentDto) {
        //取出对象变量
        UmsComment umsComment = commentDto.getUmsComment();
        if(umsComment!=null){
            UmsMember currentMember = umsMemberService.getCurrentMember();
            umsComment.setCreateBy(currentMember.getUsername());
            List<String> tagsIds = commentDto.getTagsIds();
            List<UmsView> umsViewsData = commentDto.getUmsViewsData();
            umsComment.setCreateTime(new Date());
            //首先添加评论表
            Integer i=umsCommentDao.addComment(umsComment);
            //得到返回后的评论id
            Integer commentId = umsComment.getId();
            //添加评论图片
            umsViewCommentDao.addView(commentId,umsViewsData);
            //添加标签
            umsTagsCommentDao.addTags(commentId,tagsIds);
            return i;
        }
        return null;
    }

    /**
     * 评论列表接口
     * @param productId 商品id
     * @return 评论列表
     */
    @Override
    public List<CommentDetail> list(Long productId) {
        List<CommentDetail> commentDetail = new ArrayList<>();

        // 查询主列表
        List<CommentInfo> commentDetails = umsCommentDao.list(productId);

        // 求出商品评价总数量
        String count = umsCommentDao.commentCount(productId);

        // 求出商品好评率
        String goodRate = umsCommentDao.goodRate(productId);

        // 这里应该创建 CommentDetail 对象并添加到集合中
        CommentDetail detail = new CommentDetail();
        detail.setGoodRate(goodRate);
        detail.setCommentCount(count);
        detail.setCommentInfos(commentDetails);

        // 将 detail 添加到 commentDetail 集合
        commentDetail.add(detail);

        return commentDetail;

    }
}
